//
//  ChooseController.swift
//  vmes
//
//  Created by 志鹏 翟 on 2021/12/7.
//

import Foundation
import UIKit
class ChooseController:UIAlertController,UIPickerViewDataSource,UIPickerViewDelegate{
    
    var pickerView:UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let cleanAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let startAction = UIAlertAction(title: "确定", style: .default, handler: nil)
        
        addAction(startAction)
        addAction(cleanAction)
        pickerView = UIPickerView()
        //delegate设为自己
        pickerView.delegate = self
        //DataSource设为自己
        pickerView.dataSource = self
        //设置PickerView默认值
        pickerView.selectRow(1, inComponent: 0, animated: true)
        self.view.addSubview(pickerView)
        pickerView.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalToSuperview().offset(-50)
        }
    }
    
    //设置PickerView列数(dataSourse协议)
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    //设置PickerView行数(dataSourse协议)
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return K.userInfo?.workshops?.count ?? 0
    }
    //设置PickerView选项内容(delegate协议)
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return String(row)+"-"+String(component)
    }
    
    //设置行高
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    //修改PickerView选项
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        //修改字体，大小，颜色
        var pickerLabel = view as? UILabel
        if pickerLabel == nil{
            pickerLabel = UILabel()
            pickerLabel?.font = UIFont.systemFont(ofSize: 16)
            pickerLabel?.textAlignment = .center
        }
        pickerLabel?.text = K.userInfo?.workshops?[row].workshopName ?? ""
        pickerLabel?.textColor = UIColor.blue
        return pickerLabel!
    }
    //检测响应选项的选择状态
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(String(row)+"-"+String(component))
    }
}
